/**
 * Copyright (c) 2010, QinJian
 * All rights reserved.
 */
package com.q.course.web.arrange;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.q.course.entity.arrange.Arrange;
import com.q.course.entity.arrange.Event;
import com.q.course.service.arrange.ArrangeService;

/**
 * @author 覃健,Email:j411@qq.com
 *
 */
@Controller
@RequestMapping("/arrange")
public class ArrangeController {

	@Autowired
	private ArrangeService arrangeService;
	
	@ResponseBody
	@RequestMapping( value="/save.do" , method = RequestMethod.POST )
	public Object[] saveArrange( @Valid Arrange arrange ){
		Long oldid = arrange.getId();
		ArrayList<Event> evs = arrangeService.saveArrange(arrange);
		
		if(oldid==null){
			return new Object[]{"add",evs};
		}
		else{
			return new Object[]{"update",evs};
		}
	}
	
	@ResponseBody
	@RequestMapping( value="/getarrange.do" , method = RequestMethod.GET )
	public List<Event> getArrange( @RequestParam(value = "start") long start , @RequestParam(value = "end") long end , @RequestParam(value = "uid") String uid ){
		Long tid = null;
		if(uid.matches("^[0-9]*[1-9][0-9]*$")){
			tid = Long.valueOf(uid);
		}
		return arrangeService.getArrange( start,end,tid );
	}
	
	/**
	 * 获取该班该课程上课次数
	 * @param arrange
	 * @return
	 */
	@ResponseBody
	@RequestMapping( value="/getcoursenum.do" , method = RequestMethod.GET )
	public Long[] getCourseNumber(Arrange arrange){
		return arrangeService.getCourseNumber(arrange);
	}
	
	@ResponseBody
	@RequestMapping( value="/goupdate.do" , method = RequestMethod.GET )
	public Arrange gotoUpdate(Arrange arrange){
		return arrangeService.gotoUpdate(arrange);
	}
	
	@ResponseBody
	@RequestMapping( value="/delete.do" , method = RequestMethod.GET )
	public Long delArrange( @RequestParam(value = "id") long id ){
		return arrangeService.deleteArrange(id);
	}
	
	@ResponseBody
	@RequestMapping( value="/arrangedrop.do" , method = RequestMethod.POST )
	public boolean arrangeDrop( @RequestParam(value = "id") long id , @RequestParam(value = "days") int days , @RequestParam(value = "minutes") int minutes ){
		arrangeService.arrangeDrop(id, days, minutes);
		return true;
	}
	
	@ResponseBody
	@RequestMapping( value="/searcharrange.do" , method = RequestMethod.GET )
	public List<Arrange> searchArrange(Arrange arrange){
		return arrangeService.searchArrange(arrange);
	}
}
